<?php
/**
 * Created by PhpStorm.
 * User: fengsenhua
 * Date: 2019/2/14
 * Time: 20:27
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\User;
use app\api\model\User as UserModel;
use app\api\validate\AddressNew;
use app\lib\exception\SuccessMessage;
use app\lib\exception\UserException;
use app\api\service\Token as TokenService;

class Address extends BaseController
{
    protected $beforeActionList=[
        'checkPrimaryScope'=>['only'=>'createOrUpdateAddress']
    ];
    public function createOrUpdateAddress(){
        $validate=new AddressNew();
        $validate->goCheck();
        //根据Token获取用户的uid
        //根据uid查找用户数据，判断用户是否存在，如果不存在抛出异。
        //获取用户从客户端提交的地址信息/根据用户地址信息是否存在，从而判断是添加地址还是更新地址
        $uid=TokenService::getCurrentUid();
        $user=UserModel::get($uid);
        if(!$user){
            throw new UserException();
        }
        $dataArray=$validate->getDataByRule(input('post.'));
        $userAddress=$user->address;
        if(!$userAddress){
            $user->address()->save($dataArray);
        }else{
            $user->address->save($dataArray);
        }
        return json(new SuccessMessage(),201);
    }
}
